package com.neuronbit.businessflow.intellij.ui.projectView;

import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.refactoring.rename.naming.AutomaticRenamer;
import com.intellij.refactoring.rename.naming.AutomaticRenamerFactory;
import com.intellij.usageView.UsageInfo;
import com.neuronbit.businessflow.intellij.BpmBundle;
import com.neuronbit.businessflow.intellij.index.FlowClassIndex;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.List;

/**
 * The type Flows renamer factory.
 */
public class FlowsRenamerFactory implements AutomaticRenamerFactory {
    @Override
    public boolean isApplicable(@NotNull final PsiElement element) {
        if (!(element instanceof PsiClass)) return false;
        List<PsiFile> forms = FlowClassIndex.findFormsBoundToClass(element.getProject(), (PsiClass) element);
        return forms.size() > 0;
    }

    @Override
    public String getOptionName() {
        return BpmBundle.message("rename.bound.flows");
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public void setEnabled(final boolean enabled) {
    }

    @Override
    @NotNull
    public AutomaticRenamer createRenamer(final PsiElement element, final String newName, final Collection<UsageInfo> usages) {
        return new FlowsRenamer((PsiClass) element, newName);
    }
}
